package com.kt.uokos.yimo.service.impl;

import com.kt.uokos.yimo.dao.System;
import com.kt.uokos.yimo.example.SystemExample;
import com.kt.uokos.yimo.mapper.SystemMapper;
import com.kt.uokos.yimo.service.SystemConfigService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: TODO
 * @className: SystemConfigSeerviceImpl.java
 * @author: Kang Hao
 * @date: 2022/4/29 20:15
 **/
@Service
public class SystemConfigSeerviceImpl implements SystemConfigService {

    @Resource
    private SystemMapper systemMapper;

    @Override
    public Map<String, String> listmall() {
        SystemExample example = new SystemExample();
        example.or().andKeyNameLike("yimo_mall_" + "%").andDeletedEqualTo(false);
        List<System> systemList = systemMapper.selectByExample(example);
        Map<String, String> map = new HashMap<>();
        for (System system : systemList) {
            map.put(system.getKeyName(), system.getKeyValue());
        }

        return map;
    }

    @Override
    public Map<String, String> listExpress() {
        SystemExample example = new SystemExample();
        example.or().andKeyNameLike("yimo_express_" + "%").andDeletedEqualTo(false);
        List<System> systemList = systemMapper.selectByExample(example);
        Map<String, String> map = new HashMap<>();
        for (System system : systemList) {
            map.put(system.getKeyName(), system.getKeyValue());
        }

        return map;
    }

    @Override
    public Map<String, String> listOrder() {
        SystemExample example = new SystemExample();
        example.or().andKeyNameLike("yimo_order_" + "%").andDeletedEqualTo(false);
        List<System> systems = systemMapper.selectByExample(example);
        HashMap<String, String> map = new HashMap<>();
        for (System system : systems){
            map.put(system.getKeyName(),system.getKeyValue());
        }
        return map;
    }

    @Override
    public Map<String, String> listWx() {
        SystemExample example = new SystemExample();
        example.or().andKeyNameLike("yimo_wx_"+"%").andDeletedEqualTo(false);
        List<System> list = systemMapper.selectByExample(example);
        HashMap<String, String> map = new HashMap<>();
        for (System system : list){
            map.put(system.getKeyName(),system.getKeyValue());
        }
        return map;
    }

    @Override
    public void updateExpress(Map<String, String> map) {
        for (Map.Entry<String, String> entry : map.entrySet()) {
            SystemExample example = new SystemExample();
            example.or().andKeyNameEqualTo(entry.getKey()).andDeletedEqualTo(false);
            System system = new System();
            system.setKeyName(entry.getKey());
            system.setKeyValue(entry.getValue());
            system.setUpdateTime(LocalDateTime.now());
            systemMapper.updateByExampleSelective(system, example);
        }
    }

    @Override
    public void updateMall(Map<String, String> map) {
        for (Map.Entry<String, String> entry : map.entrySet()) {
            SystemExample example = new SystemExample();
            example.or().andKeyNameEqualTo(entry.getKey()).andDeletedEqualTo(false);
            System system = new System();
            system.setKeyName(entry.getKey());
            system.setKeyValue(entry.getValue());
            system.setUpdateTime(LocalDateTime.now());
            systemMapper.updateByExampleSelective(system, example);
        }
    }

    @Override
    public void updateOrder(Map<String, String> map) {
        for (Map.Entry<String, String> entry : map.entrySet()) {
            SystemExample example = new SystemExample();
            example.or().andKeyNameEqualTo(entry.getKey()).andDeletedEqualTo(false);
            System system = new System();
            system.setKeyName(entry.getKey());
            system.setKeyValue(entry.getValue());
            system.setUpdateTime(LocalDateTime.now());
            systemMapper.updateByExampleSelective(system, example);
        }
    }

    @Override
    public void updateWx(Map<String, String> map) {
        for (Map.Entry<String, String> entry : map.entrySet()) {
            SystemExample example = new SystemExample();
            example.or().andKeyNameEqualTo(entry.getKey()).andDeletedEqualTo(false);
            System system = new System();
            system.setKeyName(entry.getKey());
            system.setKeyValue(entry.getValue());
            system.setUpdateTime(LocalDateTime.now());
            systemMapper.updateByExampleSelective(system, example);
        }
    }

    @Override
    public List<System> quaryWxShare(String KeyName) {
        SystemExample example = new SystemExample();
        example.or().andKeyNameEqualTo(KeyName);
        return  systemMapper.selectByExample(example);
    }
}
